package cn.rongdao.jrkabao.ui.cashadvance;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.inputmethod.FRView;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.cashadvance.CashAdvanceSuccessBean;
import cn.rongdao.jrkabao.bean.cashadvance.CashDebitBean;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.model.cashadvance.CashDebitModel;
import cn.rongdao.jrkabao.model.cashadvance.CashSuccessInfoModel;
import cn.rongdao.jrkabao.model.deposit.DepositSuccessModel;
import cn.rongdao.jrkabao.ui.cashadvance.adapter.CashAdvanceSuccessAdapter;
import cn.rongdao.jrkabao.ui.cashadvance.event.CashAdvanceSuccessEvent;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.CashAdvanceSuccessFooterUIComponent;
import cn.rongdao.jrkabao.ui.deposit.account.MyAccountActivity;
import cn.rongdao.jrkabao.ui.deposit.event.DepositSuccessEvent;
import cn.rongdao.jrkabao.util.CommonUtil;

/**
 * Created by jack on 2017/6/2
 */

public class CashAdvanceSuccessActivity extends BaseAutoFinishActivity {

    private static final String IS_DEPOSIT = "is_deposit";
    private static final String VERIFY_DATA_MSG = "verify_data_msg";
    private static final String CVN2 = "cvn2";
    private static final String TERM_OF_VERIFY_DATA = "term_of_verify_data";
    private static final String IS_JI_FEN = "is_ji_fen";

    private String mOrderNum;
    private boolean isDeposit;
    private String institutionId;
    private boolean isJiFen;

    public static void start(Context context, String orderNum, String institutionId, String cvn2, String termOfValidity, String verifyDataMsg, boolean type, boolean isJiFen) {
        Bundle bundle = new Bundle();
        bundle.putString(InfoConstant.ORDER_NUM, orderNum);
//        bundle.putString(CashAdvanceActivity.INSTITUTION_ID, institutionId);
        bundle.putString(VERIFY_DATA_MSG, verifyDataMsg);
        bundle.putString(CVN2, cvn2);
        bundle.putString(TERM_OF_VERIFY_DATA, termOfValidity);
        bundle.putBoolean(IS_DEPOSIT, type);
        bundle.putBoolean(IS_JI_FEN, isJiFen);
        FRStartActivity.start(context, CashAdvanceSuccessActivity.class, bundle);
    }

    public static void start(Context context, String orderNum, boolean type) {
        start(context, orderNum, "", "", "", "", type, false);
    }

    public static void start(Context context, String orderNum, String institutionId, String cvn2, String termOfValidity, String verifyDataMsg, boolean isJiFen) {
        start(context, orderNum, institutionId, cvn2, termOfValidity, verifyDataMsg, false, isJiFen);
    }

    @BindView(R.id.acs_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.acas_ptr_framelayout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;
    @BindView(R.id.acas_tv_content)
    TextView tvContent;
    @BindView(R.id.acas_iv_image)
    ImageView ivImage;
    @BindView(R.id.acas_ll_cash_advance)
    LinearLayout llCashAdvance;

    private CashAdvanceSuccessAdapter adapter;
    private CashAdvanceSuccessFooterUIComponent viewFooter;
    private String cvn2;
    private String termOfValidity;
    private String verifyDataMsg;

    @Override
    protected void initView() {
        mOrderNum = getIntent().getStringExtra(InfoConstant.ORDER_NUM);
        isDeposit = getIntent().getBooleanExtra(IS_DEPOSIT, false);
        verifyDataMsg = getIntent().getStringExtra(VERIFY_DATA_MSG);
        cvn2 = getIntent().getStringExtra(CVN2);
        isJiFen = getIntent().getBooleanExtra(IS_JI_FEN, false);
        termOfValidity = getIntent().getStringExtra(TERM_OF_VERIFY_DATA);
//        institutionId = getIntent().getStringExtra(CashAdvanceActivity.INSTITUTION_ID);
        adapter = new CashAdvanceSuccessAdapter(this);
        RecyclerAdapterWithHF mAdapter = new RecyclerAdapterWithHF(adapter);
        viewFooter = FRView.inflateLayout(this, R.layout.layout_cash_advance_success_footer);
        if (isDeposit) {
            if (viewFooter != null) {
                viewFooter.setGoMainContent("返回我的账户");
            }
        } else {
            if (viewFooter != null) {
                viewFooter.setGoMainContent("完成");
            }
        }
        mAdapter.addFooter(viewFooter);
        ptrRecyclerViewUIComponent.setBackground(R.color.cf2f2f2);
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(this));
        ptrRecyclerViewUIComponent.setCanRefresh(false);
        ptrRecyclerViewUIComponent.setAdapter(mAdapter);

        initListener();
        initData();
        initTitleBar();
    }

    private void initListener() {
        viewFooter.setOnGoMainClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toMainActivity();
            }
        });
    }

    private void initData() {
        if (isDeposit) {
            DepositSuccessModel.getInstance().execute(mOrderNum, new FRBaseObserver<CashAdvanceSuccessBean>(this, "加载中,请稍候...") {
                @Override
                protected void onBaseNext(CashAdvanceSuccessBean data) {
                    llCashAdvance.setVisibility(View.VISIBLE);
                    adapter.setDataList(data.getList());
                    tvContent.setText(data.getTip());
                    ivImage.setImageResource(R.drawable.icon_cash_advance_success);
                    viewFooter.setVisibility(View.VISIBLE);
                    EventBus.getDefault().post(new DepositSuccessEvent());
                }
            });
        } else {
            if (!FRString.isEmpty(verifyDataMsg)) {    //合力宝
                CashDebitModel.getInstance().execute(isJiFen, mOrderNum, institutionId, cvn2, termOfValidity, verifyDataMsg, new FRBaseObserver<CashDebitBean>(this, "加载中,请稍候...") {
                    @Override
                    protected void onBaseNext(CashDebitBean data) {
                        llCashAdvance.setVisibility(View.VISIBLE);
                        tvContent.setText(CommonUtil.isStatusSuccess(data.getCash_status()) ? "取现成功" : "取现失败");
                        ivImage.setImageResource(CommonUtil.isStatusSuccess(data.getCash_status()) ? R.drawable.icon_cash_advance_success
                                : R.drawable.icon_cash_advance_failure);
                        viewFooter.setVisibility(View.VISIBLE);
                        if (null != data.getCash_list() && data.getCash_list().size() > 0) {
                            int size = data.getCash_list().size();
                            viewFooter.setPaymentDate(data.getCash_list().get(size - 1).getName()
                                    , data.getCash_list().get(size - 1).getContent(), data.getTip());
                            data.getCash_list().remove(size - 1);
                        }
                        adapter.setDataList(data.getCash_list());
                        EventBus.getDefault().post(new CashAdvanceSuccessEvent());
                    }
                });
            } else {
                CashSuccessInfoModel.getInstance().execute(mOrderNum, new FRBaseObserver<CashAdvanceSuccessBean>(this, "加载中,请稍候...") {
                    @Override
                    protected void onBaseNext(CashAdvanceSuccessBean data) {
                        llCashAdvance.setVisibility(View.VISIBLE);
                        ivImage.setImageResource(R.drawable.icon_cash_advance_success);
                        tvContent.setText("取现成功");
                        viewFooter.setVisibility(View.VISIBLE);
                        if (null != data.getCash_list() && data.getCash_list().size() > 0) {
                            viewFooter.setPaymentDate(data.getCash_list().get(data.getCash_list().size() - 1).getName()
                                    , data.getCash_list().get(data.getCash_list().size() - 1).getContent(), data.getTip());
                            data.getCash_list().remove(data.getCash_list().size() - 1);
                        }
                        adapter.setDataList(data.getCash_list());
                        EventBus.getDefault().post(new CashAdvanceSuccessEvent());
                    }
                });
            }
        }
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("交易结果");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toMainActivity();
            }
        });
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_cash_advance_success;
    }

    private void toMainActivity() {
        if (isDeposit) {
            MyAccountActivity.start(thisActivity());
        } else {
//            CashAdvanceActivity.start(thisActivity(), true);
        }
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            toMainActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}